package view;

import java.util.Observable;
import java.util.Observer;

import model.Holder;
import model.Machine;

import com.VanDoren.ArgusV2.R;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MachineView extends LinearLayout implements Observer 
{
	private static final String TAG = MachineView.class.getSimpleName();
	
	private TextView _machineName;
	private TextView _productOrder;
	private TextView _product;
	private TextView _count;
	private TextView _speed;
	
	private ProgressBar _loader;
	private ProgressBar _countBar;
	
	private TextView _errorView;
	
	public MachineView(Context context) 
	{
		super(context);
		initializeView(context);
	}
	
	public MachineView(Context context, AttributeSet attrs) 
	{
		super(context, attrs);
		initializeView(context);
	}

	public MachineView(Context context, AttributeSet attrs, int defStyle) 
	{
		super(context, attrs, defStyle);
		initializeView(context);
	}
	
	private void initializeView(Context context) 
	{		
		LayoutInflater inflater = LayoutInflater.from(context);
		inflater.inflate(R.layout.machine_view, this, true);
		
		setBackgroundResource(R.drawable.vdm_blue_solid);
		setOrientation(LinearLayout.HORIZONTAL);
		
		setChildren();
	}
	
	private void setChildren()
	{
		RelativeLayout layout = (RelativeLayout) getChildAt(0);
		
		_machineName = (TextView) layout.getChildAt(5);
		_productOrder = (TextView) layout.getChildAt(6);
		_product = (TextView) layout.getChildAt(7);
		_count = (TextView) layout.getChildAt(8);
		_speed = (TextView) layout.getChildAt(9);
		_loader = (ProgressBar) layout.getChildAt(10);
		_countBar = (ProgressBar) layout.getChildAt(11);
		_errorView = (TextView) layout.getChildAt(12);
	}
	
	private void removeLoader()
	{
		_loader.setVisibility(INVISIBLE);
		
		_machineName.setVisibility(VISIBLE);
		_productOrder.setVisibility(VISIBLE);
		_product.setVisibility(VISIBLE);
		_count.setVisibility(VISIBLE);
		_speed.setVisibility(VISIBLE);
		_countBar.setVisibility(VISIBLE);
	}
	
	private void updateHolder(Holder holder)
	{
		Rect surface = holder.getSurface();
		
		setX(surface.left - surface.width());
		setY(surface.top - surface.height());
		
		if(holder.getMachine() == null)
			showErrorMessage(holder.getErrorMessage());
		else
			updateMachine(holder.getMachine());
	}
	
	private void showErrorMessage(String error)
	{
		if(error.isEmpty())
			return;
		
		
		_loader.setVisibility(INVISIBLE);
		_errorView.setVisibility(VISIBLE);
		_errorView.setText(error);
		
	}
	
	private void updateMachine(Machine machine)
	{
		Log.d(TAG, String.format("Update Machine: %s", machine));
		if(machine == null)
			return;
		removeLoader();
		
		_machineName.setText(machine.getName());
		_productOrder.setText(machine.getOrder().getID());
		_product.setText(machine.getOrder().getActualProduct());
		_count.setText(String.format("%d / %d", machine.getOrder().getActualCount(), machine.getOrder().getRequestedCount()));
		_speed.setText(String.format("%d", (int) machine.getCurrentSpeed()));
		
		_countBar.setMax(machine.getOrder().getRequestedCount());
		_countBar.setProgress(machine.getOrder().getActualCount());
	}

	@Override
	public void update(Observable observable, Object data) 
	{
		Log.d(TAG, String.format("Observable: %s", observable.toString()));
		
		updateHolder((Holder) data);
	}

}
